#include "mythread.h"
#include <QByteArray>
#include <QHostAddress>
#include "formsrv.h"
MyThread::MyThread(int ID, QObject *parent) :
    QThread(parent)
{
    socket == NULL;
    this->SocketDescriptor = ID;
}

void MyThread::run()
{
    socket = new QTcpSocket();
    if(!socket->setSocketDescriptor(SocketDescriptor))
    {
        emit sig_error(socket->error());
        return;
    }
    qDebug()<<"start thread:"<<SocketDescriptor;
    connect(socket,SIGNAL(readyRead()),this,SLOT(slot_readReady()));
    connect(socket,SIGNAL(disconnected()),this,SLOT(slot_disconnect()));
    qDebug()<<"Client connected...";
    this->exec();
}

void MyThread::slot_readReady()
{
    QByteArray   data = socket->readAll();
    qDebug()<<"Data in:"<<socket->peerAddress().toString()<<":"<<data;
    QString str = "Thread ID:"+QString::number(SocketDescriptor)+" "+socket->peerAddress().toString()+":"+QVariant(data).toString();
    //getForm()->slot_DisplayMsg(SocketDescriptor,0,str);
    socket->write(data);
}

void MyThread::slot_disconnect()
{
    qDebug()<<"Client disconnect...";
    socket->deleteLater();
    exit(0);
}

void MyThread::slot_connect()
{
}
